package com.ftg.wymall.controller;

import com.alibaba.fastjson2.JSONObject;
import com.ftg.wymall.comm.vo.RespMessUtil;
import com.ftg.wymall.service.UploadService;
import io.lettuce.core.RedisClient;
import io.lettuce.core.api.StatefulRedisConnection;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * 上传功能
 *
 * @author kangjx
 * @version 1.0
 * @date 2023/11/16 上午8:59
 */

@RestController
@RequestMapping("/upload")
@Api(tags = "上传功能")
public class UploadController {

    @Resource
    private UploadService uploadService;

    /**
     * 上传头像
     *
     * @param avatarFile 头像文件
     * @return 响应结果对象，包含上传结果信息
     */
    @PostMapping(value = "/uploadAvatar")
    public RespMessUtil<JSONObject> uploadAvatar(
           @ApiParam("avatarFile") @RequestParam("avatarFile") MultipartFile avatarFile
    ) {
        return uploadService.uploadAvatar(avatarFile);
    }


    /**
     * http://198.18.0.21/upload/avatar/5f818854-1b39-4255-b632-4cdab6ca6d5a.png
     * 删除头像功能,根据现有的头像地址信息，
     *    信息是个人中心修改时获得的数据表中头像
     * @param avatarFileUrl 头像文件地址
     * @return 响应结果对象，包含上传结果信息
     */
    @PostMapping("/delAvatar")
    public RespMessUtil<JSONObject> delAvatar(
            @ApiParam("avatarFileUrl") @RequestParam("avatarFileUrl") String avatarFileUrl
    ) {
        return uploadService.delAvatar(avatarFileUrl);
    }


    @GetMapping("/test")
    public String test() {
        return "test";
    }


//    public static void main(String[] args) {
//        RedisClient redisClient = RedisClient.create("redis://localhost/2");
//        StatefulRedisConnection<String, String> connection = redisClient.connect();
//
//        System.out.println("Connected to Redis");
//        connection.sync().set("hello", "Hello World");
//        connection.sync().setex("aa",3000,"ttt");
//
//        System.out.println(connection.sync().get("hello"));
//
//        connection.close();
//        redisClient.shutdown();
//    }

    @Resource
    @Qualifier("redisTemplate")
    RedisTemplate redisTemplate;

    @PostMapping("/hello")
    public String set(@RequestParam("key") String key, @RequestParam("name") String name) {
        System.out.println(key);
        redisTemplate.opsForValue().set(key,name);
        return "success";
    }


}
